﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SD.App.Desktop.Utils
{
    public static class ValidateRules
    {
        // 必填校验
        public static string Required(string value, string field) => string.IsNullOrWhiteSpace(value) ? $"{field}不能为空" : string.Empty;

        // 长度校验
        public static string Length(string value, int min, int max, string field)
        {
            if (string.IsNullOrWhiteSpace(value)) return string.Empty;
            return value.Length < min || value.Length > max ? $"{field}需{min}-{max}位" : string.Empty;
        }

        // 密码强度（字母+数字）
        public static string Password(string pwd) => !Regex.IsMatch(pwd, @"^(?=.*[A-Za-z])(?=.*\d).+$") ? "需包含字母和数字" : string.Empty;

        // 邮箱格式
        public static string Email(string email)
        {
            if (string.IsNullOrWhiteSpace(email)) return string.Empty;
            return Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$") ? string.Empty : "邮箱格式错误";
        }
    }
}
